// pages/home/home.js
import { getSwiperList } from '../../api/homeApi'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopName: '',
    // 轮播图内容
    swiperArr: [],
    // 九宫格内容
    gridArr: [
      {
        id: 1,
        icon: '/images/grid/fruit.png',
        text: '果蔬'
      },
      {
        id: 2,
        icon: '/images/grid/clothing.png',
        text: '服装'
      },
      {
        id: 3,
        icon: '/images/grid/toy.png',
        text: '玩具'
      },
      {
        id: 4,
        icon: '/images/grid/nibbles.png',
        text: '零食'
      },
      {
        id: 5,
        icon: '/images/grid/hotel.png',
        text: '酒店'
      },
      {
        id: 6,
        icon: '/images/grid/numerical.png',
        text: '数码'
      },
      {
        id: 7,
        icon: '/images/grid/furniture.png',
        text: '家具'
      },
      {
        id: 8,
        icon: '/images/grid/other.png',
        text: '其它'
      }
    ],
    testImgArr: [
      {
        id: 2001,
        url: '/images/testimg/test01.jpg',
        name: '氨基酸洗面奶',
        price: 39.9
      },
      {
        id: 2002,
        url: '/images/testimg/test02.jpg',
        name: '毛孔修复液',
        price: 83
      },
      {
        id: 2003,
        url: '/images/testimg/test03.jpg',
        name: '车灯清洗剂',
        price: 103.4
      },
      {
        id: 2004,
        url: '/images/testimg/test04.jpg',
        name: '男士剃须刀',
        price: 97.8
      },
      {
        id: 2005,
        url: '/images/testimg/test05.jpg',
        name: '秋季保暖外套',
        price: 137
      }
    ]
  },
  // 发起网络请求
  async getDataList() {
    const { data: res } = await wx.p.request({
      url: 'url',
      method: 'post',
      data: {
        name: 'lsp',
        age: 24
      }
    })
    console.log(res)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.getDataList
    this.getSwiperDataList()
  },

  // 点击搜索进入到搜索页面
  goToSearch() {
    wx.navigateTo({
      url: '/homepackage/pages/search/search',
    })
  },
  // 输入框点击事件处理函数
  getSearchValue(event) {
    var name = this.data.shopName = event.detail
    console.log("输入的商品为：" + name)
  },

  // 九宫格每一项点击事件
  classCilck(e) {
    //获取文本内容
    const text = e.target.dataset.item.text
    wx.navigateTo({
      url: '/homepackage/pages/shoplist/shoplist',
    })
  },
  // 点击推荐商品进入到商品详情页
  shopDetails(e) {
    // 获取选中的索引值
    const index = e.currentTarget.dataset['index'];
    // 获取商品id
    const id = this.data.testImgArr[index].id
    //跳转到商品详情页
    wx.navigateTo({
      url: '/homepackage/pages/shopdetails/shopdetails?id=' + id,
    })
  },

  /**
   * 获取轮播图
   */
  getSwiperDataList() {
    getSwiperList().then(res => {
      console.log(res.data.message)
      this.setData({
        swiperArr: res.data.message
      })
    })
  },

  /**
   * 轮播图跳转到商品详情页面
   */
  goShopList(e) {
    const id = e.currentTarget.dataset['id']
    wx.navigateTo({
      url: '/homepackage/pages/shopdetails/shopdetails?id=' + id,
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
})